package edu.gatech.coc.sirana.graph;

import java.util.ArrayList;
import java.util.HashMap;

import net.util.Debug;

import edu.gatech.coc.sirana.parser.ClientLogItem;

public class FDANode implements Comparable<FDANode>{
	private int id; // the order of being added to a graph
	private String action;
	private ArrayList<ClientLogItem> visitingHisotry;
	
	private HashMap<String, FDAEdge> inEdges = new HashMap<String, FDAEdge>() ;
	private HashMap<String, FDAEdge> outEdges= new HashMap<String, FDAEdge>();
	
	public FDANode(ClientLogItem cli){
		this.action = cli.findAction();		
		this.visitingHisotry	= new ArrayList<ClientLogItem>();	
		this.addVisitingHistory(cli);
	}
	
	public void addVisitingHistory(ClientLogItem cli){
		this.visitingHisotry.add(cli);
	}
	
	public ClientLogItem findLastVisit(){		
		return this.visitingHisotry.get(this.visitingHisotry.size()-1);
	}
	
	public HashMap<Integer, FDANode> findOutgoingNodes(){
		HashMap<Integer, FDANode> oNodes = new HashMap<Integer, FDANode>();
		
		for (String eLabel : outEdges.keySet()){
			FDAEdge e = outEdges.get(eLabel);
			FDANode sink = e.getSink();
			oNodes.put(sink.getId(), sink);
		}
		
		return oNodes;
	}
	
	public HashMap<Integer, FDANode> findIncomingNodes(){
		HashMap<Integer, FDANode> iNodes = new HashMap<Integer, FDANode>();
		
		for (String eLabel: inEdges.keySet()){			
			FDAEdge e = inEdges.get(eLabel);
			FDANode source = e.getSource();
			iNodes.put(source.getId(), source);
		}
		
		return iNodes;
	}
	
	public boolean isSame(FDANode e){
		if (e==null) return false;
		return this.getId() == e.getId();
	}
		
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	
	public ArrayList<ClientLogItem> getVisitingHisotry() {
		return visitingHisotry;
	}
	public void setVisitingHisotry(ArrayList<ClientLogItem> visitingHisotry) {
		this.visitingHisotry = visitingHisotry;
	}
	
	public void addInEdge(FDAEdge e){
		this.inEdges.put(e.findLabel(), e);
	}	
	public void addOutEdge(FDAEdge e){
		this.outEdges.put(e.findLabel(), e);
	}
	

	public HashMap<String, FDAEdge> getInEdges() {
		return inEdges;
	}

	public void setInEdges(HashMap<String, FDAEdge> inEdges) {
		this.inEdges = inEdges;
	}

	public HashMap<String, FDAEdge> getOutEdges() {
		return outEdges;
	}

	public void setOutEdges(HashMap<String, FDAEdge> outEdges) {
		this.outEdges = outEdges;
	}

	@Override
	public int compareTo(FDANode c) {
		if (this.getId() > c.getId()) return 1;
		else if (this.getId() == c.getId()) return 0;
		else return -1;
	}	
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("node: " + this.getId() + "\t" + this.getAction());
		return sb.toString();
	}
}
